﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common.FileManager;
using System.Collections.Generic;
using System.IO;
using V5_Common;
using V5_Common.WaterMark;

namespace V5_EShop.Admin.Picture {
    public partial class Picture_Manage_Dialog : BasicPage {
        const string vPath = "vPath";
        string uploadPath = "";
        protected void Page_Load(object sender, EventArgs e) {

            if (!this.IsPostBack) {
                uploadPath = InputHelper.SetString(Request["uploadfiles"]);
                if (uploadPath != "") {
                    ViewState[vPath] = uploadPath;
                }
                this.Bind_PathAll();
                this.Bind_dlList();
            }
        }

        private void Bind_dlList() {
            string tempStr = "";
            if (ViewState[vPath] != null && InputHelper.SetString(ViewState[vPath]) != "UpLoadFiles") {
                tempStr = "UpLoadFiles/" + InputHelper.SetString(ViewState[vPath]);
            }
            else {
                tempStr = "UpLoadFiles/";
            }
            FileSystemManager.SetRootPath(Server.MapPath("~/" + tempStr));
            FileSystemManager.ReadPath(false);
            List<FileSystemItem> list = FileSystemManager.GetItems();

            Pager.RecordCount = list.Count;
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = list;
            pds.AllowPaging = true;
            pds.CurrentPageIndex = Pager.CurrentPageIndex - 1;
            pds.PageSize = Pager.PageSize;

            this.dlPicList.DataSource = pds;
            this.dlPicList.DataBind();
        }

        private void Bind_PathAll() {
            DirectoryInfo[] directories = new DirectoryInfo(Server.MapPath("~/UpLoadFiles")).GetDirectories();
            if (directories.Length > 0) {
                string str10 = string.Empty;
                for (int i = 0; i < directories.Length; i++) {
                    this.ddlSelectPicPath.Items.Add(new ListItem(directories[i].Name.ToLower(), directories[i].Name.ToLower()));
                }
            }
            this.ddlSelectPicPath.Items.Insert(0, new ListItem("根目录", "UpLoadFiles"));
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            this.Bind_dlList();
        }

        protected void ddlSelectPicPath_SelectedIndexChanged(object sender, EventArgs e) {
            string eName = this.ddlSelectPicPath.SelectedValue;
            ViewState[vPath] = eName;
            this.Bind_dlList();
        }

        public string Bind_Pic(object o) {
            string tempStr = InputHelper.SetString(o.ToString()).ToLower().Trim();
            if (tempStr.Length > 0) {
                tempStr = tempStr.Replace(".jpg", "");
                tempStr = tempStr.Replace(".png", "");
                tempStr = tempStr.Replace(".gif", "");
            }
            return tempStr;
        }
        //上传文件
        protected void btnUpload_Click(object sender, EventArgs e) {
            if (IsAllowedExtension(FileUploadGo)) {
                string path1 = Server.MapPath("~/UpLoadFiles/productL/");//大图
                string path2 = Server.MapPath("~/UpLoadFiles/productM/");//中图
                string path3 = Server.MapPath("~/UpLoadFiles/productS/");//小图
                string UpFilePath = FileUploadGo.PostedFile.FileName;
                string extension = Path.GetExtension(UpFilePath);
                string fileName = Path.GetFileNameWithoutExtension(UpFilePath);
                fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
                path1 = path1 + fileName + extension;
                path2 = path2 + fileName + extension;
                path3 = path3 + fileName + extension;

                FileUploadGo.PostedFile.SaveAs(path1);//保存大图
                WaterMark wm = new WaterMark();

                //int mwidth = InputHelper.SetInt(config.UploadMwidth);
                //if (mwidth < 1) {
                int mwidth = 400;
                //}
                //int swidth = InputHelper.SetInt(config.UploadSwidth);
                //if (swidth < 1) {
                int swidth = 100;
                //}

                wm.MakeThumbnail(path1, path2, mwidth, 100, "W");//生成中图
                wm.MakeThumbnail(path1, path3, swidth, 100, "W");//生成中图

                this.Bind_dlList();
            }
        }

        public bool IsAllowedExtension(HtmlInputFile hifile) {
            string strOldFilePath = "", strExtension = "";
            string[] arrExtension = { ".gif", ".jpg", ".jpeg", ".bmp", ".png", ".swf" };
            if (hifile.PostedFile.FileName != string.Empty) {
                strOldFilePath = hifile.PostedFile.FileName.ToLower();
                strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
                for (int i = 0; i < arrExtension.Length; i++) {
                    if (strExtension.Equals(arrExtension[i])) {
                        return true;
                    }
                }
            }
            return false;
        }

        public string Bind_PicPath(object o) {
            string tempStr = InputHelper.SetString(o);
            string rStr = "";
            string path = InputHelper.SetString(ViewState[vPath]).ToLower();
            if (tempStr.Length > 0) {
                if (path != "uploadfiles") {
                    rStr = path + "/" + tempStr;
                }
                else {
                    rStr = tempStr;
                }
            }
            return rStr;
        }
    }
}
